import json

from util.common import *

if __name__ == '__main__':

    list = []

    df = read_excel("source/tf_smp_web_asset2.xlsx")
    filtered_df = df[df['PUBLIC_WEB_IP'].notna()]

    for createTime in generate_workdays('2024-01-01', '2024-11-28', 1):
        id = "CLODZX" + random_string(14)

        timeSpan = random.randrange(2, 20)
        start_date, end_date = get_random_date(createTime, timeSpan)

        random_record = filtered_df.sample(n=1)

        assetId = random_record['ID'].item()
        pIP = random_record["PUBLIC_WEB_IP"].item()
        iIP = random_record["INTRANET_IP"].item()
        iPort = random_record["INTRANET_PORT"].item()

        ipscope = json.dumps([{
            "ip": iIP,
            "domain": pIP,
            "id": assetId
        }])

        seep = {
            "taskId": id,
            "taskName": f"检查任务-{pIP}-{createTime[:10]}",
            "startTime": start_date,
            "endTime": end_date,
            "ipScope": ipscope,
            "creator": 'PORT6452766823027939',
        }
        message1 = render_template("""
           INSERT INTO tf_smp_url_probe_task(task_id, task_name, request_pact, port_scop, url_suffix, url_suffix_desc,
                                  PUBLISH_TYPE, task_rate, start_time, CRON_DESC, end_time, TASK_STATUS, SWITCH_OF_404,
                                  task_desc, creator, creat_date, updator, update_date, URL_TOTAL_NUM, IP_SCOPE,
                                  SWITCH_OF_BLANK_PAGE, TASK_MAIN_ID, TASK_TYPE)
            VALUES ($taskId,
                    $taskName, NULL, NULL, 'URL02183209139492850', 'portal', '0', '0',
                    $startTime, '{"exeType":"1"}',
                    $endTime, '30', '0', NULL, 
                    $creator,
                    $startTime, 
                    $creator,
                    $endTime, '10',
                    $ipScope, '0',
                    NULL, '1');
        """, seep)

        list.append(message1)

        report = {
            "id": id,
            "taskId": id,
            "startTime": start_date
        }

        message2 = render_template(
            """
                   INSERT INTO tf_smp_url_probe_exe(EXE_ID, TASK_ID, EXE_CUST_TIME, EXE_STATUS, EXE_PRO, EXE_FAIL_REASON, EXE_DESC,
                                 CREATE_TIME, UPDATE_TIME)
                    VALUES ($id, $taskId, '687401', '30', '0', NULL, '执行完成', $startTime, NULL);
                """,
            report)
        list.append(message2)

        for item in read_sql_file('source/tf_smp_url_probe_result.sql'):
            seep = {
                "id": "ADDRZX" + random_string(14),
                "exeId": id,
                "taskId": id,
                "ip": iIP,
                "port": iPort,
                "startTime": start_date,
                "endTime": end_date,
                "iIP": iIP,
            }
            list.append(render_template(item, seep))

    write_file('target/SATP_应用地址探测造数.sql', list)
